public class Solution949 {

    String ans="";
    int maxH=0,maxM=0;

    public String largestTimeFromDigits(int[] arr) {
        int[] result=new int[4];
        boolean[] visit=new boolean[4];
        dfs(arr,result,visit,0);
        return ans;
    }

    private void dfs(int[] arr, int[] result, boolean[] visit, int k) {
        if (k == 4) {
            int a = result[0] * 10 + result[1];
            int b = result[2] * 10 + result[3];
            if (a < 24 && b <= 59) {
                if (ans.equals("") || a > maxH || a == maxH && b > maxM) {
                    maxH = a;
                    maxM = b;
                    String ta=(a<10)?"0".concat(String.valueOf(a)):String.valueOf(a);
                    String tb=(b<10)?"0".concat(String.valueOf(b)):String.valueOf(b);
                    ans = ta.concat(":").concat(tb);
                }
            }
            return;
        }
        for (int i = 0; i < 4; i++) {
            if (!visit[i]) {
                visit[i] = true;
                result[k] = arr[i];
                dfs(arr, result, visit, k + 1);
                visit[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution949().largestTimeFromDigits(new int[]{1,2,3,4}));
    }
}
